<?php
/**
 * @author Hayduk Iruna haydukiryna@gmail.com
 * System 'Abiturient'
 */

require 'Zend/Loader/Autoloader.php';

class Bootstrap
{
    private $_config = null;

    protected $_request = null;

    public function run($config)
    {
        try {
            $this->setLoader();
            $this->setConfig($config);
            //$this->setView();
            $this->setDbAdapter();
            $route = $this->setRouter();
            $front = Zend_Controller_Front::getInstance();
            $front->setBaseUrl($this->_config->url->base)
              ->throwExceptions(true)
              ->setRouter($route);
            $front->run(array(
                             'admin'=>$this->_config->path->application.'controllers/Admin',
                             'default'=>$this->_config->path->application.'controllers/Frontend'
            ));
        } catch (Exception $e) {
            Error::catchException($e);
        }
    }

    public function setLoader()
    {
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->setFallbackAutoloader(true);
    }

    public function setConfig($config)
    {
        $cacheDir = $config['path']['cache'];
        $configFilesDir = $config['path']['application'].'config/';
        $configModel = new Config_Abstract();
        $fileConfig = $configModel->getConfig($cacheDir, $configFilesDir);
        $config = array_merge_recursive($config,$fileConfig);
        $config = new Zend_Config($config);
        $this->_config = $config;
        Zend_Registry::set('config', $config);
    }

    public function setView() 
    {
        Zend_Layout::startMvc(array(
                                'layoutPath' => $this->_config->path->layouts,
                                'layout' => 'page-1',
        ));
        $layout = Zend_Layout::getMvcInstance();
        $view = $layout->getView();

        $layout->setViewSuffix('phtml');
        $view->baseUrl = $this->_config->url->base;

        $view->setBasePath($this->_config->path->views);
        $view->addHelperPath($this->_config->path->helper,'App_Helper');
        $layout->setView($view);

        $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
        $viewRenderer
            ->setView($view)
            ->setViewSuffix('phtml');

        Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
    }

    public function setDbAdapter() 
    {
        $db = Zend_Db::factory($this->_config->db);
        Zend_Db_Table_Abstract::setDefaultAdapter($db);
        Zend_Registry::set('db', $db);
    }

    public function setRouter() 
    {
        require($this->_config->path->autoload->system . 'Routers.php');
        if (!($router instanceof Zend_Controller_Router_Abstract)) {
            throw new Exception('Incorrect config file: routes');
        }

        return $router;
    }

    public static function getStoreConfig()
    {
        return Zend_Registry::get('store_config');
    }

    public static function setStoreConfig($config)
    {
        Zend_Registry::set('store_config', $config);
    }

    public static function getRequest()
    {
        $request = Zend_Registry::get('request');
        return $request;
    }

    public static function getConfig()
    {
        return Zend_Registry::get('config');
    }
/**
 * @todo Виправити функцію
 * Enter description here ...
 * @param unknown_type $path
 */
    public static function getUrl($path=null)
    {
        $config = Zend_Registry::get('config');
        return $config->url->base.$path;
    }
}